﻿#include "SearchBinaryTree.h"

void test01()
{
	int a[] = { 8, 3, 1, 10, 6, 4, 7, 14, 13 };
	BSTree<int> b1;
	for (auto e : a)
	{
		b1.Insert(e);
	}

	b1.InOrder();

	b1.Insert(16);
	b1.InOrder();
	b1.Insert(2);
	b1.InOrder();
}

void test02()
{
	int a[] = { 8, 3, 1, 10, 6, 4, 7, 14, 13 };
	BSTree<int> b1;
	for (auto e : a)
	{
		b1.Insert(e);
	}

	cout << b1.Find(10) << endl;
	cout << b1.Find(8) << endl;
	cout << b1.Find(2) << endl;
	cout << b1.Find(11) << endl;
}

void test03()
{
	int a[] = { 8, 3, 1, 10, 6, 4, 7, 14, 13 };
	BSTree<int> b1;
	for (auto e : a)
	{
		b1.Insert(e);
	}

	b1.InOrder();

	//b1.erase(4);
	//b1.InOrder();
	//b1.erase(10);
	//b1.InOrder();
	//b1.erase(6);
	//b1.InOrder();
	//b1.erase(6);
	//b1.InOrder();
	//b1.erase(8);
	//b1.InOrder();
	//b1.erase(3);
	//b1.InOrder();
	//b1.erase(14);
	//b1.InOrder();
	//b1.erase(1);
	//b1.InOrder();

	for (auto e : a)
	{
		b1.erase(e);
		b1.InOrder();
	}
}

namespace key
{
	void test04()
	{
		BSTree<string, string> dict;
		//BSTree<string, string> copy = dict;
		dict.Insert("left", "左边");
		dict.Insert("right", "右边");
		dict.Insert("insert", "插⼊");
		dict.Insert("string", "字符串");
		string str;
		while (cin >> str)
		{
			auto ret = dict.Find(str);
			if (ret)
			{
				cout << "->" << ret->_value << endl;
			}
			else
			{
				cout << "无此单词，请重新输入" << endl;
			}
		}
	}

	void test05()
	{
		string arr[] = { "苹果", "西瓜","苹果", "苹果", "西瓜", "苹果", "苹果", "西瓜", "苹果", "香蕉", "苹果"};
		BSTree<string, int> countTree;
		for (const auto& str : arr)
		{
			// 先查找⽔果在不在搜索树中
			// 1、不在，说明⽔果第⼀次出现，则插⼊<⽔果, 1>
			// 2、在，则查找到的结点中⽔果对应的次数++
			//BSTreeNode<string, int>* ret = countTree.Find(str);
			auto ret = countTree.Find(str);
			if (ret == NULL)
			{
				countTree.Insert(str, 1);
			}
			else
			{
				ret->_value++;
			}
		}
		countTree.InOrder();
	}
}

int main()
{
	//test01();
	//test02();
	//test03();
	//key::test04();
	key::test05();
	return 0;
}